﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using Accounting.Library;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatTaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhomTaiKhoan;
using Converter;
using System.Resources;
using System.Reflection;

namespace csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan
{
    public partial class DM_TaiKhoan_ListView : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            // no parameters
        }

        public string Title
        {
            get { return "TaiKhoan"; }
        }

        #endregion

        #region Load Methods
        public DM_TaiKhoan_ListView()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.DM_TaiKhoan_GeneralInformation_GroupBox, controls);
            this.DM_TaiKhoan_GridView.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource dm_TaiKhoan_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                dm_TaiKhoan_collectionViewSource.Source = new List<object> { dm_TaiKhoan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TinhChatTaiKhoan_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatTaiKhoan_ListViewModelSource"];
                DM_TinhChatTaiKhoan_ListViewModel dm_TinhChatTaiKhoan_ListViewModel = new DM_TinhChatTaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatTaiKhoan_collectionViewSource.Source = new List<object> { dm_TinhChatTaiKhoan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_NhomTaiKhoan_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhomTaiKhoan_ListViewModelSource"];
                DM_NhomTaiKhoan_ListViewModel dm_NhomTaiKhoan_ListViewModel = new DM_NhomTaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_NhomTaiKhoan_collectionViewSource.Source = new List<object> { dm_NhomTaiKhoan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoan_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ViewModelSource"];
                DM_TaiKhoan_ViewModel dm_TaiKhoan_ViewModel = new DM_TaiKhoan_ViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                
                dm_TaiKhoan_ViewSource.Source = new List<object> { dm_TaiKhoan_ViewModel };
                dm_TaiKhoan_ViewModel.Saved += new EventHandler(dm_TaiKhoan_ViewModel_Saved);
              


            }
        }
        #endregion
        #region  Event Handle
        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {

            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }

        private void DM_TaiKhoan_GeneralInformation_NhomTaiKhoanId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhomTaiKhoan_ChildWindow dm_NhomTaiKhoan_ChildWindow = new DM_NhomTaiKhoan_ChildWindow();
            dm_NhomTaiKhoan_ChildWindow.Show();
            dm_NhomTaiKhoan_ChildWindow.SaveClicked += new EventHandler(dm_NhomTaiKhoan_ViewModel_Saved);         
        }

        private void DM_TaiKhoan_GeneralInformation_TinhChatTaiKhoanId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_TinhChatTaiKhoan_ChildWindow dm_TinhChatTaiKhoan_ChildWindow = new DM_TinhChatTaiKhoan_ChildWindow();
            dm_TinhChatTaiKhoan_ChildWindow.Show();
            dm_TinhChatTaiKhoan_ChildWindow.SaveClicked += new EventHandler(dm_TinhChatTaiKhoan_ViewModel_Saved);
        }

        #endregion
        #region View Model Related Methods
        void dm_TaiKhoan_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel vm = ((List<object>)dm_TaiKhoan_ListViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            vm.GetDM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
        }


        void dm_TinhChatTaiKhoan_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TinhChatTaiKhoan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatTaiKhoan_ListViewModelSource"];
            DM_TinhChatTaiKhoan_ListViewModel vm = ((List<object>)dm_TinhChatTaiKhoan_ListViewSource.Source)[0] as DM_TinhChatTaiKhoan_ListViewModel;
            vm.GetDM_TinhChatTaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_NhomTaiKhoan_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhomTaiKhoan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhomTaiKhoan_ListViewModelSource"];
            DM_NhomTaiKhoan_ListViewModel vm = ((List<object>)dm_NhomTaiKhoan_ListViewSource.Source)[0] as DM_NhomTaiKhoan_ListViewModel;
            vm.GetDM_NhomTaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        private void OnCellDoubleClick(object sender, RadRoutedEventArgs args)
        {
            if (this.DM_TaiKhoan_GridView.SelectedItem != null)
            {
                System.Windows.Data.CollectionViewSource dm_TaiKhoan_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ViewModelSource"];
                DM_TaiKhoan_ViewModel vm = ((List<object>)dm_TaiKhoan_ViewSource.Source)[0] as DM_TaiKhoan_ViewModel;
                int parameter = ((DM_TaiKhoanInfo)this.DM_TaiKhoan_GridView.SelectedItem).Khoa;
                Csla.Xaml.ExecuteEventArgs eventObject = new Csla.Xaml.ExecuteEventArgs();
                eventObject.MethodParameter = parameter;
                vm.GetDM_TaiKhoan(this, eventObject);
            }
        }
        #endregion

    }
}
